package rz.time;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class SimpleDateFormatTest {

	/**
	 * @param args
	 * @throws Exception
	 */
	public static void main(String[] args) throws Exception {

		timezoneConv();
	}

	
	/**
	 * vm会取操作系统的时区和时间,如果设置了vm的时区与操作系统不一样,-Duser.timezone=GMT+7
	 * 那vm会根据系统的时间换算成vm时区的时间,如系统为GMT+8 12:00,vm中new Date()时间为11:00
	 * 2011-4-1 下午07:56:47
	 * @author
	 * @throws Exception
	 */
	public static void timeZone() throws Exception {
		String[] timeZones = TimeZone.getAvailableIDs();
		for (String str : timeZones) {
			// System.out.println(str);
		}
		TimeZone timeZone = TimeZone.getTimeZone("GMT+09:00");// 比系统时间提前一个小时
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(timeZone);

		Calendar calendar = Calendar.getInstance(timeZone);
		Date date = calendar.getTime();
		System.out.println(date);
		System.out.println(new Date());
		System.out.println(calendar.getTime());

		System.out.println(sdf.format(new Date()));// 输出格式化后的时间
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(sdf.format(new Date())));
		System.out.println(TimeZone.getDefault().getID());
		System.out.println("+++++++++++++++++++++++++++==");
	
		Calendar cal = Calendar.getInstance();
		System.out.println(cal.getTime());
		TimeZone tzCN = TimeZone.getDefault(); // TimeZone.getTimeZone("Asia/Shanghai");
		TimeZone tzJP = TimeZone.getTimeZone("Japan");
		TimeZone tzES = TimeZone.getTimeZone("Europe/Madrid");
		TimeZone tzES2 = TimeZone.getTimeZone("GMT+1:00");
		Locale locCN = Locale.CHINA;
		Locale locJP = Locale.JAPAN;
		Locale locES = new Locale("es");
		Locale locES2 = Locale.US;
		Calendar calCN = Calendar.getInstance(tzCN);
		Calendar calJP = Calendar.getInstance(tzJP);
		Calendar calES = Calendar.getInstance(tzES);
		Calendar calES2 = Calendar.getInstance(tzES);
		// long iDate = cal.getTimeInMillis();
		// long iOffsetCN = tzCN.getOffset(iDate);
		// long iOffsetJP = tzJP.getOffset(iDate);
		// long iOffsetES = tzES.getOffset(iDate);
		// long iOffsetES2 = tzES2.getOffset(iDate);
		sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setCalendar(calCN);
		System.out.println(tzCN.getDisplayName(locCN) + "\t\t"
				+ sdf.format(cal.getTime()));
		sdf.setCalendar(calJP);
		System.out.println(tzJP.getDisplayName(locJP) + "\t\t"
				+ sdf.format(cal.getTime()));
		sdf.setCalendar(calES);
		System.out.println(tzES.getDisplayName(locES) + "\t"
				+ sdf.format(cal.getTime()));
		sdf.setCalendar(calES2);
		System.out.println(tzES2.getDisplayName(locES2) + "\t\t"
				+ sdf.format(cal.getTime()));
	}

	public static void sdf() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			System.out.println(sdf.parse("2010-6-5"));// 此日期的时间为00:00:00
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	/*
	 * 格林威治时间转换成其它时区时间
	 */
	public static void timezoneConv() {
		java.util.GregorianCalendar   ca   =   new   java.util.GregorianCalendar(java.util.TimeZone.getTimeZone("GMT+00:00"));

		ca.set(2012, 3, 2, 0, 0, 0);
		/*
		    1333324800671
			1333324800781
			1333324800109
			1333324800875
		*/
		System.out.println(ca.getTimeInMillis());//毫秒数是一样的,与时区无关

		java.text.SimpleDateFormat   format=new   java.text.SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");

		format.setTimeZone(TimeZone.getTimeZone("GMT+8"));

		System.out.println(format.format(ca.getTime()));
	}
	
	public static void timePrint() {

		Locale   localeEN   =   Locale.US;
		Locale   localeFrance   =   Locale.FRANCE;

		TimeZone   timeZoneMiami   =   TimeZone.getDefault();
		TimeZone   timeZoneParis   =   TimeZone.getTimeZone( "Europe/Paris");

		DateFormat   dateFormatter   =   DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,localeEN);
		DateFormat   dateFormatterParis   =   DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,localeFrance);

		Date   curDate   =   new   Date();

		System.out.println( "Display   for   Miami   office. ");
		//   Print   the   Miami   time   zone   display   name   in   English
		System.out.println(timeZoneMiami.getDisplayName(Locale.CHINA));
		//   Set   the   time   zone   of   the   dateFormatter   to   Miami   time   zone.
		dateFormatter.setTimeZone(timeZoneMiami);
		//   Print   the   formatted   date.
		System.out.println(dateFormatter.format(curDate));

		//   Set   the   time   zone   of   the   date   formatter   to   Paris   time   zone.
		dateFormatter.setTimeZone(timeZoneParis);
		//   Print   the   Paris   time   zone   display   name   in   English.
		System.out.println(timeZoneParis.getDisplayName(localeEN) +  "aaa");
		//   Print   the   Paris   time   in   english.
		System.out.println(dateFormatter.format(curDate));

		System.out.println("\nDisplay   for   Paris   office. ");
		//   Print   the   Miami   time   zone   display   name   in   French
		System.out.println(timeZoneMiami.getDisplayName(localeFrance));
		//   Set   the   timezone   of   the
		//   dateFormatterParis   to   Miami   time   zone.
		dateFormatterParis.setTimeZone(timeZoneMiami);
		//   Print   the   formatted   date   in   French.
		System.out.println(dateFormatterParis.format(curDate));

		//   Set   the   timezone   of   the   date   formatter   to   Paris   time   zone.
		dateFormatterParis.setTimeZone(timeZoneParis);
		//   Print   the   Paris   time   zone   display   name   in   French.
		System.out.println(timeZoneParis.getDisplayName(localeFrance));
		//   Print   the   Paris   time   in   French.
		System.out.println(dateFormatterParis.format(curDate)); 
	}
}
